<?php
/**
 * 通用函数类
 * @author Zeyes <lixize8888@163.com>
 * @link http://www.zeyes.org
 * @copyright (c) 2015-2017 DotTimes
 * @version 1.0
 */

require_once(dirname(__FILE__).'/../config.php');
require_once('Db.class.php');

class Comm
{
    /**
     * 连接数据库
     * @return Object 数据库类实例
     */
    public static function connDb()
    {
        $config = array('dbms' => 'mysql', 'host' => DB_HOST,
            'database' => DB_NAME, 'user' => DB_USER, 'pass' => DB_PASSWD);
        $obj = new Db($config);
        $obj->connect();
        return $obj;
    }

    /**
     * 计算两个日期的时间间隔
     * @param 
     *      string $start 开始的时间
     *      string $end   结束的时间
     * @return int 时间间隔
     */
    public static function dateCount($start, $end)
    {
        $datetime1 = new DateTime($start);
        $datetime2 = new DateTime($end);
        $interval = date_diff($datetime1, $datetime2);
        return intval($interval->format('%R%a'));
    }

    /**
     * 生成url请求字符串， 类似http_build_query
     * @param array 数组
     * @return string 字符串
     */
    public static function a2s($arr)
    {
        $str = "";
        foreach ($arr as $key => $value)
        {
            if (is_array($value))
            {
                foreach ($value as $value2)
                {
                    $str .= urlencode($key) . "[]=" . urlencode($value2) . "&";
                }
            }
            else
            {
                $str .= urlencode($key) . "=" . urlencode($value) . "&";
            }
        }
        return $str;
    }

    /**
     * url字符串生成数组
     * @param string 字符串
     * @return array 数组
     */
    public static function s2a($str)
    {
        $arr = array();
        parse_str($str, $arr);
        return $arr;
    }
}
?>